﻿using Essensoft.Paylink.WeChatPay;
using Microsoft.AspNetCore.Http.Features;
using RedPacket.Utils;
using Serilog;


/// <summary>
/// 全局引入
/// </summary>
public static class CustomInject
{
    /// <summary>
    /// 全局注入
    /// </summary>
    /// <param name="services"></param>
    public static void AddCustom(this IServiceCollection services)
    {
        //定时任务
        services.AddCustomJobs();

        //配置上传文件的大小
        services.Configure<FormOptions>(e => e.MultipartBodyLengthLimit = int.MaxValue);

        //全局异常注入
        services.AddMvcFilter<GlobalException>();

        services.AddWeChatPay();

        services.Configure<WeChatPayOptions>(App.Configuration.GetSection("WeChatPay"));

        //全局时间格式
        services.AddControllers().AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
        });

        //动态控制器
        services.AddDynamicApiControllers();

        //缓存注入
        RedisHelper.Initialization(new CSRedis.CSRedisClient(App.Configuration.GetSection("redis").Value));

   

        services.AddInject();

        services.AddEventBus();

        services.AddJwt();

        services.AddSqlsugarSetup();

        services.AddCorsAccessor();
    }

    /// <summary>
    /// 全局引入
    /// </summary>
    /// <param name="app"></param>
    public static void UseCustom(this IApplicationBuilder app)
    {
        app.UseScheduleUI(opt => opt.RequestPath = "/app-job");

        app.UseInject();

        app.UseCorsAccessor();

        app.UseAuthentication();

        app.UseAuthorization();

        (app as WebApplication)?.MapControllers();
    }
}

